const axios = require('axios')
const { JWTgetToken, JWTgetInfo } = require(':JWT')
const itemList = require(':models/itemList')
const { client_id, client_secret } = require(':config')

const reponseJson={code:20000,data:"",total:0}

class itemListCtrl {
  async findlist(ctx) {
    const itemslist = await itemList.find();
    // console.log(11,ctx.state.userInfo)
    ctx.body = {...reponseJson,data:{itemslist},total:itemslist.length}
  }
  async findById(ctx) {
    console.log(ctx.params.id)

    // const { fields } = ctx.query
    // console.log('find by id', fields)
    // // 把 f 不存在的字段过滤掉
    // const selectFields = fields.split(';').filter(f => f).map(item => ' +' + item).join('')
    const item = await itemList.findById(ctx.params.id) // +employments +educations
    ctx.body = item
  }
  async create(ctx) {
    const item = await new itemList({login: 'xv700666' }).save()
    ctx.body = item
  }
  async update(ctx) {
    // db[ctx.params.id * 1] = ctx.request.body
    ctx.verifyParams({
      name: { type: 'string', required: true },
      avatar_url: { type: 'string', required: false },
      locations: { type: 'array', itemType: 'string', required: false },
      employments: { type: 'array', itemType: 'object', required: false }
    })
    const user = await User.findByIdAndUpdate(ctx.params.id, ctx.request.body)
    if (!user) ctx.throw(404, errorMessage)
    ctx.body = user
  }
  async del(ctx) {
    // db.splice(ctx.params.id * 1, 1)
    const user = await User.findByIdAndRemove(ctx.params.id)
    // ctx.status = 204
    if (!user) ctx.throw(404, errorMessage)
  }



}

module.exports = new itemListCtrl()
